package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mysql.SundayFreight;
import org.itboys.core.service.mysql.web.SundayFreightService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/16
 * Description:
 */

@Controller
@RequestMapping("/sunday/web/freight")
public class SundayFreightController extends BaseController {
    @Autowired
    private SundayFreightService freightService;

    /**
     * 时间段首页
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        HttpServletResponse response
    ) {
        return "/sunday/freight/index";
    }


    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {
            PageResult<SundayFreight> result = freightService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/freight/select------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    /**
     * 新增或修改常用问答
     *
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value = "id") Long id,
                     @RequestParam(value = "totalPrice") BigDecimal totalPrice,
                     @RequestParam(value = "minPrice") BigDecimal minPrice,
                     @RequestParam(value = "maxPrice") BigDecimal maxPrice
    ) {
        try {
            SundayFreight freight = new SundayFreight(totalPrice,minPrice, maxPrice);

            freightService.save(id, freight);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = " ";
            if (e.getClass().getSimpleName().equalsIgnoreCase("Error Exception")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/freight/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value = "id", required = true) Long id

    ) {
        try {
            freightService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/freight/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = true) Long id
    ) {
        try {

            SundayFreight freight = freightService.findOne(id);
            AjaxUtils.renderJson(response, freight);
        } catch (Exception e) {
            e.printStackTrace();

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/freight/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }
    
}
